package com.hbpa.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.hbpa.dao.DaduiAccPageDao;
import com.hbpa.form.DaduiLoginAcc;
import com.hbpa.util.ConnectionFactory;
import com.hbpa.util.JdbcUtil;

public class DaduiAccPageDaoImpl implements DaduiAccPageDao {
		public final int Dadui_page = 10; 
		
		public List<DaduiLoginAcc> DaduiLogAcc() {
			int startItem;
			int endItem; 
			int totalSize = getTotalSize();
			List<DaduiLoginAcc> accs = null;
			
			Connection con = null;
			PreparedStatement ps = null;
			ResultSet rs = null;
			DaduiLoginAcc acc = null;
			try {
				accs = new ArrayList<DaduiLoginAcc>();
				con = ConnectionFactory.getConnection(); 
				ps = con.prepareStatement("select * from dd_acc order by id");
				
				rs = ps.executeQuery();
				while(rs.next())
				{ 
					 acc = new DaduiLoginAcc(); 
					 acc.setXm(rs.getString("xm"));
					 acc.setSsdd(rs.getString("ssdd"));
					 acc.setPass(rs.getString("pass"));
					 accs.add(acc);
				}
				 return accs;
		} catch (SQLException e) { 
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs,ps,con);
		} 
			return null;
		}

		public List<DaduiLoginAcc> getDaduiLogAccByPage(int page) {
			int startItem;
			int endItem; 
			int totalSize = getTotalSize();
			List<DaduiLoginAcc> accs = null;
			
			if(page>getTotalPage())
			{
				return null;
			}else if(page == 1 && totalSize < Dadui_page*page){
				startItem = 0; 
				endItem = totalSize;
			}else if(totalSize < Dadui_page*page){
				startItem = Dadui_page*(page-1); 
				endItem = totalSize-Dadui_page*(page-1);
			}else{
				startItem = Dadui_page*(page-1); 
				endItem =10;
			}
			
			Connection con = null;
			PreparedStatement ps = null;
			ResultSet rs = null;
			DaduiLoginAcc acc = null;
			try {
				accs = new ArrayList<DaduiLoginAcc>();
				con = ConnectionFactory.getConnection(); 
				ps = con.prepareStatement("select * from dd_acc order by id asc limit ?,?");
				
				ps.setInt(1, startItem);
				ps.setInt(2, endItem);
				
				rs = ps.executeQuery();
				
				while(rs.next())
				{ 
					 acc = new DaduiLoginAcc();
					 acc.setXm(rs.getString("xm"));
					 acc.setSsdd(rs.getString("ssdd"));
					 acc.setPass(rs.getString("pass"));
					 accs.add(acc);
				}
				 return accs;
		} catch (SQLException e) { 
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs,ps,con);
		} 
			return null;
		}

		public int getTotalPage() {
			Connection con = null;
			Statement st = null;
			ResultSet rs = null;
			try {
				con = ConnectionFactory.getConnection(); 
				st = con.createStatement();
				String sql = "select count(*) as c from dd_acc order by id desc";
				rs = st.executeQuery(sql);
				if(rs.next())
				{ 
					String size = rs.getString("c");
					if(!size.equals("0")){
						int count = Integer.parseInt(size);
						if(count%Dadui_page == 0)
						{
							return count/Dadui_page;
						}else{
							return count/Dadui_page+1;
						}
					}else{
						return 0;
					}
				}
			} catch (SQLException e) { 
				e.printStackTrace();
			}finally{
				JdbcUtil.close(rs,st,con);
			}
			return 0;
		}

		public int getTotalSize() {
			Connection con = null;
			Statement st = null;
			ResultSet rs = null;
			try {
				con = ConnectionFactory.getConnection(); 
				st = con.createStatement();
				String sql = "select count(*) as c from dd_acc order by id desc";
				
				rs = st.executeQuery(sql);
				if(rs.next())
				{ 
					String count = rs.getString("c");
					if(!count.equals("0")){
						return Integer.parseInt(count);
					}else{
						return 0;
					}
				}
			} catch (SQLException e) { 
				e.printStackTrace();
			}finally{
				JdbcUtil.close(rs,st,con);
			}
			return 0;
		}
		
		public boolean deleteDaduiLogAcc(String xm) {
			Connection con = null;
			PreparedStatement ps = null;
			try {
				con = ConnectionFactory.getConnection(); 
				ps = con.prepareStatement("delete from dd_acc where xm=?");
				
				ps.setString(1, xm);
				boolean isDelete = ps.execute();
				
				return isDelete;
			} catch (SQLException e) { 
				e.printStackTrace();
			}finally{
				JdbcUtil.close(ps,con);
			}
			return false;
		}
	}

   
